package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Admin;
import com.ejuornal.bean.Page;
import com.ejuornal.dao.AdminDao;

/**
 * 项目名称：ejuornal 类名称：AdminDaoImpl 类描述： 管理员管理持久层实现类 创建人：陈晨 创建时间：Jan 26, 2013
 * 3:44:13 AM 修改人：陈晨 修改时间：Jan 26, 2013 3:44:13 AM 修改备注：
 * 
 * @version
 */
@Component("adminDao")
public class AdminDaoImpl extends DaoSupport<Admin> implements AdminDao {
	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void batchDelete(String sql, int... ids) {
		super.batchDelete(sql, ids);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	@Override
	public Page<Admin> find(int current_record, String url) {
		return super.find(current_record, url);
	}

	public Admin validate(Admin admin) {
		String sql = "select * from t_admin t where t.account=?";
		try {
			admin = super.getJdbcTemplate().queryForObject(sql,
					new Object[] { admin.getAccount() },
					new RowMapper<Admin>() {
						public Admin mapRow(ResultSet rs, int rowNum) {
							Admin admin = new Admin();
							try {
								admin.setAccount(rs.getString("account"));
								admin.setAdminId(rs.getInt("adminId"));
								admin.setPassword(rs.getString("password"));
								admin.setRoleId(rs.getInt("roleId"));
							} catch (SQLException e) {
								e.printStackTrace();
							}
							return admin;
						}
					});
		} catch (Exception e) {
			System.out.println("用户名不存在！！！"); // 处理用户不存在的情况
			admin = null;
		}

		return admin;
	}
	

	public boolean resetPwd(Admin admin, String newPwd) {
		String sql = "update t_admin t set t.password=? where t.account=?";
		int i = super.getJdbcTemplate()
				.update(sql,
						new Object[] { newPwd, admin.getAccount()});
		if (i == 1) {
			return true;
		}
		return false;
	}

	public Admin getAdminById(int  adminId) {
		String sql = "select * from t_admin where adminId = ?";
		Admin admin = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { adminId }, new RowMapper<Admin>() {
					public Admin mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Admin admin = new Admin();
					
						admin.setAccount(rs.getString("account"));
						admin.setAdminId(rs.getInt("adminId"));
						admin.setPassword(rs.getString("password"));
						admin.setRoleId(rs.getInt("roleId"));
						return admin;
					}
				});
		return admin;
	}

	/*public Page<Admin> findAll(int current_record, String url) {
		Page<Admin>  page=super.find(current_record, url);
		
		return page;
	}*/

}
